package com.one.monitoring.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.google.gson.Gson;
import com.one.monitoring.service.IALiYunSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * @author lxh
 * @description 阿里云短信
 * @time 2023/11/17 12:04
 **/
@Service
@Slf4j
public class ALiYunSmsServiceImpl implements IALiYunSmsService {
    /**
     * AccessKey ID
     */
    public static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5tBcSupMLtJikMEY8Nvx";
    /**
     * AccessKey Secret
     */
    public static final String ALIBABA_CLOUD_ACCESS_KEY_SECRET = "Nfzi5dup2B6cnwcjBP7Fv2rqNB7Hhd";
    /**
     * 签名
     */
    public static final String SIGN_NAME = "鲸云创想";
    /**
     * 模版CODE
     */
    public static final String TEMPLATE_CODE = "SMS_462240241";
    /**
     * 发送短信
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    @Override
    public Integer SendSmsCode(String phone, String code) {
        Integer integer;
        try {
            integer = sendCode(phone, code);
            if (integer < 2){
                log.info("给:{}的短信已发出-----------",phone);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return integer;
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId key
     * @param accessKeySecret 秘钥
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送操作
     * @param phoneNumber 手机号
     * @param code 验证码
     * @return
     * @throws Exception
     */
    public static Integer sendCode(String phoneNumber, String code) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        com.aliyun.dysmsapi20170525.Client client = createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(SIGN_NAME)
                .setTemplateCode(TEMPLATE_CODE)
                .setPhoneNumbers(phoneNumber)
                .setTemplateParam(jsonObject.toJSONString());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String info = new Gson().toJson(sendSmsResponse);
            JSONObject resultJson = JSONObject.parseObject(info);
            log.info("短信发送结果：{}",resultJson);
            if ("OK".equals(resultJson.getJSONObject("body").getString("code"))){
                return 1;
            }
            return 2;
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            log.error("短信发送失败",error.message);
            return 2;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            log.error("短信发送失败",error.message);
            com.aliyun.teautil.Common.assertAsString(error.message);
            return 2;
        }
    }
}
